{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "TclError",
     "evalue": "couldn't recognize data in image file \"D:\\\\b02561.jpg\"",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTclError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 13\u001b[0m\n\u001b[0;32m     11\u001b[0m canvas \u001b[38;5;241m=\u001b[39m tk\u001b[38;5;241m.\u001b[39mCanvas(window, bg\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mblue\u001b[39m\u001b[38;5;124m'\u001b[39m, height\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m100\u001b[39m, width\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m400\u001b[39m)\n\u001b[0;32m     12\u001b[0m \u001b[38;5;66;03m# 自带的PhotoImage只能使用gif图片\u001b[39;00m\n\u001b[1;32m---> 13\u001b[0m image_file \u001b[38;5;241m=\u001b[39m \u001b[43mtk\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mPhotoImage\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mD:\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43mb02561.jpg\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     14\u001b[0m \u001b[38;5;66;03m# 0,0 -> 锚定的点, anchor='nw' -> 左上角锚定\u001b[39;00m\n\u001b[0;32m     15\u001b[0m image \u001b[38;5;241m=\u001b[39m canvas\u001b[38;5;241m.\u001b[39mcreate_image(\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m0\u001b[39m, anchor\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnw\u001b[39m\u001b[38;5;124m'\u001b[39m, image\u001b[38;5;241m=\u001b[39mimage_file)\n",
      "File \u001b[1;32md:\\Ananocnda\\envs\\Digital_image_processing\\Lib\\tkinter\\__init__.py:4146\u001b[0m, in \u001b[0;36mPhotoImage.__init__\u001b[1;34m(self, name, cnf, master, **kw)\u001b[0m\n\u001b[0;32m   4141\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, name\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, cnf\u001b[38;5;241m=\u001b[39m{}, master\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkw):\n\u001b[0;32m   4142\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Create an image with NAME.\u001b[39;00m\n\u001b[0;32m   4143\u001b[0m \n\u001b[0;32m   4144\u001b[0m \u001b[38;5;124;03m    Valid resource names: data, format, file, gamma, height, palette,\u001b[39;00m\n\u001b[0;32m   4145\u001b[0m \u001b[38;5;124;03m    width.\"\"\"\u001b[39;00m\n\u001b[1;32m-> 4146\u001b[0m     \u001b[43mImage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mphoto\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcnf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmaster\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkw\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Ananocnda\\envs\\Digital_image_processing\\Lib\\tkinter\\__init__.py:4093\u001b[0m, in \u001b[0;36mImage.__init__\u001b[1;34m(self, imgtype, name, cnf, master, **kw)\u001b[0m\n\u001b[0;32m   4091\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m cnf\u001b[38;5;241m.\u001b[39mitems():\n\u001b[0;32m   4092\u001b[0m     options \u001b[38;5;241m=\u001b[39m options \u001b[38;5;241m+\u001b[39m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m-\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m+\u001b[39mk, v)\n\u001b[1;32m-> 4093\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtk\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcall\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mimage\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mcreate\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mimgtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   4094\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname \u001b[38;5;241m=\u001b[39m name\n",
      "\u001b[1;31mTclError\u001b[0m: couldn't recognize data in image file \"D:\\\\b02561.jpg\""
     ]
    }
   ],
   "source": [
    "\n",
    "import tkinter as tk\n",
    "\n",
    "# 实例化一个窗口对象\n",
    "window = tk.Tk()\n",
    "# 设置title\n",
    "window.title('my window')\n",
    "# 设置大小,XxY的字符串格式\n",
    "window.geometry('600x400')\n",
    "\n",
    "\n",
    "canvas = tk.Canvas(window, bg='blue', height=100, width=400)\n",
    "# 自带的PhotoImage只能使用gif图片\n",
    "image_file = tk.PhotoImage(file=r'D:\\\\b02561.jpg')\n",
    "# 0,0 -> 锚定的点, anchor='nw' -> 左上角锚定\n",
    "image = canvas.create_image(0, 0, anchor='nw', image=image_file)\n",
    "\n",
    "# 画布上画一条线\n",
    "x0, y0, x1, y1 = 50, 50, 80, 80\n",
    "line = canvas.create_line(x0, y0, x1, y1)\n",
    "# 画一个红色的圆\n",
    "oval = canvas.create_oval(x0, y0, x1, y1, fill='red')\n",
    "# 画一个正方形\n",
    "rect = canvas.create_rectangle(100, 30, 100 + 20, 30 + 20)\n",
    "\n",
    "\n",
    "def moveit():\n",
    "    # 将rect对象向下移动\n",
    "    canvas.move(rect, 0, 2)\n",
    "\n",
    "\n",
    "canvas.pack()\n",
    "b = tk.Button(window, text='move', command=moveit).pack()\n",
    "\n",
    "window.mainloop()  # 循环显示\n",
    "\n",
    "\n",
    "\n",
    "from PIL import ImageTk,Image\n",
    "# 针对其他格式的对象需要借助PIL处理图像,通过tk.Label实现\n",
    "window2 = tk.Tk()\n",
    "window2.geometry('600x400')\n",
    "im = Image.open(r'C:\\Users\\Administrator\\Desktop\\1.jpg')\n",
    "img = ImageTk.PhotoImage(im)\n",
    "tk.Label(window2,image=img,width=300,height=300).pack()\n",
    "window2.mainloop()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Digital_image_processing",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
